package justbuy.item.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.type.TypeReference;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableProperty;
import org.ironrhino.core.aop.PublishAware;
import org.ironrhino.core.model.BaseTreeableEntity;
import org.ironrhino.core.util.JsonUtils;

/*
 * 商品类目
 */
@PublishAware
@Searchable(alias = "category")
public class Category extends BaseTreeableEntity<Category> {

	private static final long serialVersionUID = -6104554677196094404L;

	private List<PropertyTemplate> propertyTemplates = new ArrayList<PropertyTemplate>();

	public Category() {
	}

	public Category(String name) {
		this.name = name;
	}

	public Category(String name, int displayOrder) {
		this.name = name;
		this.displayOrder = displayOrder;
	}

	public List<PropertyTemplate> getPropertyTemplates() {
		return propertyTemplates;
	}

	public void setPropertyTemplates(List<PropertyTemplate> propertyTemplates) {
		if (propertyTemplates != null) {
			Iterator<PropertyTemplate> it = propertyTemplates.iterator();
			while (it.hasNext()) {
				if (it.next() == null)
					it.remove();
			}
		}
		this.propertyTemplates = propertyTemplates;
	}

	public String getPropertyTemplatesAsString() {
		if (propertyTemplates == null || propertyTemplates.isEmpty())
			return null;
		return JsonUtils.toJson(propertyTemplates);
	}

	public void setPropertyTemplatesAsString(String str) {
		if (StringUtils.isNotBlank(str))
			try {
				propertyTemplates = JsonUtils.fromJson(str,
						new TypeReference<List<PropertyTemplate>>() {
						});
			} catch (Exception e) {
				e.printStackTrace();
			}
	}

	@Override
	@SearchableProperty(boost = 2)
	public String getFullname() {
		return super.getFullname();
	}

	@Override
	public String toString() {
		return getFullname();
	}

}
